package com.atguigu.ggkt.wechat.controller;


import com.alibaba.fastjson.JSONObject;
import com.atguigu.ggkt.model.wechat.Menu;
import com.atguigu.ggkt.response.Result;
import com.atguigu.ggkt.vo.wechat.MenuVo;
import com.atguigu.ggkt.wechat.service.MenuService;
import com.atguigu.ggkt.wechat.utils.ConstantPropertiesUtil;
import com.atguigu.ggkt.wechat.utils.HttpClientUtils;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单明细 订单明细 前端控制器
 * </p>
 *
 * @author zfj
 * @since 2022-07-10
 */
@RestController
@RequestMapping("/admin/wechat/menu")
public class MenuController {

	@Autowired
	private MenuService menuService;

	@ApiOperation(value = "删除菜单")
	@DeleteMapping("removeMenu")
	public Result removeMenu() throws WxErrorException {
		menuService.removeMenu();
		return Result.ok(null);
	}

	//同步菜单的方法
	@GetMapping("syncMenu")
	public Result creatMenu() throws WxErrorException {
		menuService.syncMenu();
		return Result.ok(null);
	}

	//获取access_token
	@GetMapping("getAccessToken")
	public Result getAccessToken() {
		try {
			//拼接请求地址
			StringBuffer buffer = new StringBuffer();
			buffer.append("https://api.weixin.qq.com/cgi-bin/token");
			buffer.append("?grant_type=client_credential");
			buffer.append("&appid=%s");
			buffer.append("&secret=%s");
			//请求地址设置参数
			String url = String.format(buffer.toString(),
					ConstantPropertiesUtil.ACCESS_KEY_ID,
					ConstantPropertiesUtil.ACCESS_KEY_SECRET);
			//发送http请求
			String tokenString = HttpClientUtils.get(url);
			//获取access_token
			JSONObject jsonObject = JSONObject.parseObject(tokenString);
			String access_token = jsonObject.getString("access_token");
			//返回
			return Result.ok(access_token);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail(null);
		}
	}

	//获取所有菜单，按照一级和二级菜单封装
	@GetMapping("findMenuInfo")
	public Result findMenuInfo() {
		List<MenuVo> list = menuService.findMenuInfo();
		return Result.ok(list);
	}

	//获取所有一级菜单
	@GetMapping("findOneMenuInfo")
	public Result findOneMenuInfo() {
		List<Menu> list = menuService.findMenuOneInfo();
		return Result.ok(list);
	}

	@ApiOperation(value = "获取")
	@GetMapping("get/{id}")
	public Result get(@PathVariable Long id) {
		Menu menu = menuService.getById(id);
		return Result.ok(menu);
	}

	@ApiOperation(value = "新增")
	@PostMapping("save")
	public Result save(@RequestBody Menu menu) {
		menuService.save(menu);
		return Result.ok(null);
	}

	@ApiOperation(value = "修改")
	@PutMapping("update")
	public Result updateById(@RequestBody Menu menu) {
		menuService.updateById(menu);
		return Result.ok(null);
	}

	@ApiOperation(value = "删除")
	@DeleteMapping("remove/{id}")
	public Result remove(@PathVariable Long id) {
		menuService.removeById(id);
		return Result.ok(null);
	}

	@ApiOperation(value = "根据id列表删除")
	@DeleteMapping("batchRemove")
	public Result batchRemove(@RequestBody List<Long> idList) {
		menuService.removeByIds(idList);
		return Result.ok(null);
	}
}
